package com.atzwc.aop.annotation.aspect;


import com.atzwc.aop.annotation.utils.LoggerUtils;
import org.aspectj.bridge.IMessage;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(-1)
public class LoggerAspect {

    @Pointcut("execution(* com.atzwc.aop.annotation.target.*.*(..))")
    public void zqkyes() {

    }

//    @Before("execution( public int com.atzwc.aop.annotation.target.MathImpl.add(int int))")
//    @Before("execution(* com.atzwc.aop.annotation.target.*.*(..))")
    @Before("zqkyes()")
    public void  beforeAdviceMethod(JoinPoint joinPoint) {
//        System.out.println("前置通知，methodName"+joinPoint.getSignature().getName(),joinPoint.getArgs());
//            System.out.println("前置通知,methodName:"+ joinPoint.getSignature().getName()+"args:"+joinPoint.getArgs());
        LoggerUtils.showMethodNameAndArgs(joinPoint.getSignature().getName(),joinPoint.getArgs());

    }

    @After("zqkyes()")
    public void afterAdviceMethod(){
        System.out.println("我有我的后置通知yesyes----》");
    }

    @AfterReturning(value = "zqkyes()",returning = "re")
    public void afterReturningAdviceMethod(JoinPoint joinPoint,Object re) {
        LoggerUtils.showMethodNameAndResult(joinPoint.getSignature().getName(),re);
    }

    @AfterThrowing(value = "zqkyes()",throwing ="ex")
    public void afterThrowingAdviceMethod(JoinPoint joinPoint,Exception ex) {
        LoggerUtils.showMethodNameAndExceptionMessage(joinPoint.getSignature().getName(),ex.getMessage());

    }

    @Around("zqkyes()")
    public Object aroundAdviceMethod(ProceedingJoinPoint joinPoint) {
        Object object = null;
        try {
            System.out.println("Around-->前置");

            object = joinPoint.proceed();
            System.out.println("Around-->返回通知");
    } catch (Throwable throwable) {
            System.out.println("Around-->异常通知");
            throwable.printStackTrace();
        }finally {
            System.out.println("Around-->后置通知");
        }
        return object;

    }

}
